package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.admin.pojo.SStudentTree;
import com.zhentao.service.SStudentTreeService;
import com.zhentao.mapper.SStudentTreeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author song-xinhang
* @description 针对表【s_student_tree】的数据库操作Service实现
* @createDate 2024-07-24 11:28:16
*/
@Service
public class SStudentTreeServiceImpl extends ServiceImpl<SStudentTreeMapper, SStudentTree>
    implements SStudentTreeService{

    @Resource
    private SStudentTreeMapper sStudentTreeMapper;

    //树形查询
    @Override
    public List<SStudentTree> treslist() {
        List<SStudentTree> lists = this.sStudentTreeMapper.selectClassTree();
        List<SStudentTree> trees = new ArrayList<>();
        //存放一级
        lists.forEach(ss->{
            if(ss.getParentId() == 0){
                trees.add(getChildren(ss,lists));
            }
        });
        return trees;
    }

    //添加子菜单
    public SStudentTree getChildren(SStudentTree studentTree,List<SStudentTree> treeList){
        studentTree.setChildren(new ArrayList<>());
        treeList.forEach(menu->{
            //当子节点为 空设置为空数组
            if(menu.getChildren() == null){
                menu.setChildren(new ArrayList<>());
            }
            //当前id与父级id相同添加当前元素
            if(studentTree.getId() == menu.getParentId()){
                studentTree.getChildren().add(getChildren(menu, treeList));
            }
        });
        return studentTree;
    }
}




